﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
namespace XW.Utils
{
    public static class CryptoHelper
    {
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string MD5(string source) {
            return BytesUtil.ToBase64(Md5CryptoUtil.Encrypt(BytesUtil.FromString(source)));
        }

        public static string MD5Str(string source)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(source)), 4, 8);
            t2 = t2.Replace("-", "");
            t2 = t2.ToLower();
            return t2;
        }

        public static string MD5Encrypt32(string str)
        {
            string pwd = BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(Encoding.Default.GetBytes(str))).Replace("-", "").ToLower();
            return pwd;
        }

        /// <summary>
        ///  U9 密码加密
        /// </summary>
        /// <param name="password">The password.</param>
        /// <param name="encryptType">Type of the encrypt.</param>
        /// <returns></returns>
        public static string GetPasswordMD5Value(string password, string encryptType)
        {
            string empty = string.Empty;
            string empty2 = string.Empty;
            string s;
            if (encryptType == "b")
            {
                byte[] bytes = Convert.FromBase64String(password);
                s = Encoding.Default.GetString(bytes);
            }
            else
            {
                s = password;
            }
            UnicodeEncoding unicodeEncoding = new UnicodeEncoding();
            byte[] bytes2 = unicodeEncoding.GetBytes(s); 
            bytes2 = Md5CryptoUtil.Encrypt(bytes2);
            return Convert.ToBase64String(bytes2);
        }
        /// <summary>
        /// DES 加密
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string DESEncrypt(string source) {

            return BytesUtil.ToBase64(DesCryptoUtil.Encrypt(BytesUtil.FromString(source)));
        }

        /// <summary>
        /// DES 解密
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string DESDecrypt(string source)
        {

            return BytesUtil.ToString(DesCryptoUtil.Decrypt(BytesUtil.FromBase64(source)));
        }

        /// <summary>
        /// Base64 加密
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns></returns>
        public static string Base64Encrypt(string source) {
            return BytesUtil.ToBase64(BytesUtil.FromString(source));
        }
        /// <summary>
        /// Base64 解密.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns></returns>
        public static string Base64Decrypt(string source) {

            return BytesUtil.ToString(BytesUtil.FromBase64(source));
        }
    }
}
